package com.example.fish.utils;

import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

@Component
public class SensitiveWordFilter {
    private Set<String> sensitiveWords;

    public SensitiveWordFilter() {
        sensitiveWords = new HashSet<>();
        sensitiveWords.add("坏蛋");
        sensitiveWords.add("笨蛋");
        sensitiveWords.add("你妈");
        sensitiveWords.add("傻叉");
        sensitiveWords.add("傻B");
    }
    /***
     * 判断是否含有敏感词
     * @return  true/false
     */
    public boolean containsSensitiveWord(String text) {
        for (String word : sensitiveWords) {
            if (text.contains(word)) {
                return true;
            }
        }
        return false;
    }

    /***
     * 替换敏感词
     * @return 替换后的字符串
     */
    public String filterSensitiveWord(String text) {
        for (String word : sensitiveWords) {
            text = text.replaceAll(word, "**");
        }
        return text;
    }

    /***
     * 违规行为+1
     */
    public void addSensitiveWordCount() {

    }
    public static void main(String[] args) {
        String text = "你这个小课啊、";
        SensitiveWordFilter filter = new SensitiveWordFilter();
        String s = filter.filterSensitiveWord(text);
        System.out.println("替换之后的内容："+s);
        boolean b = filter.containsSensitiveWord(text);
        System.out.println("是否包含敏感词："+b);
    }

}
